package com.e2oto.seller.modules.seller.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.e2oto.seller.R;
import com.e2oto.seller.common.base.CommonAdapter;
import com.e2oto.seller.common.utils.ImageLoader;
import com.e2oto.seller.common.utils.ViewHolderUtils;
import com.e2oto.seller.modules.seller.bean.SelectImageBean;

import java.util.ArrayList;
import java.util.List;

import me.iwf.photopicker.PhotoPicker;
import me.iwf.photopicker.PhotoPreview;


/**
 * Created by ZhangLiQiang on 2017/9/22.
 */

public class SelectImageAdapter extends BaseAdapter {

    private List<SelectImageBean> mDatas;
    private Context mContext;
    private int maxSelectImageNumber = 4;

    public SelectImageAdapter(Context mContext, int maxSelectImageNumber) {
        this.mDatas = new ArrayList<>() ;
        this.mContext = mContext;
        this.maxSelectImageNumber = maxSelectImageNumber;
        //添加图片的btn
        SelectImageBean initBean = new SelectImageBean();
        initBean.setAddBtn(true);
        initBean.setPath("");
        mDatas.add(initBean);
    }

    /***
     * 添加图片都以这个为入口
     * @param
     */
    public void addOneImage(String path) {
        SelectImageBean bean = new SelectImageBean(false, path);
        mDatas.add(mDatas.size() - 1, bean);
        maxSelectImageNumber--;
        if (maxSelectImageNumber == 0) {
           mDatas.remove(mDatas.size() - 1);
        }

        notifyDataSetChanged();
    }

    /***
     * 拿除加号外的所有图片
     * @return
     */
    public List<String> getStringList() {
        List<String> list = new ArrayList<>();
        for (SelectImageBean bean : mDatas) {
            if (!bean.isAddBtn()) {
                list.add(bean.getPath());
            }
        }
        return list;
    }

    /***
     * 直接添加服务器返回的List<String>
     * @param list
     */
    public void addStringList(List<String> list) {

        for (int i = 0; i < list.size(); i++) {
            addOneImage(list.get(i));
        }
    }


    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public SelectImageBean getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        convertView=View.inflate(mContext,R.layout.item_select_image,null);
        SelectImageBean bean=getItem(position);
        ImageView iv = (ImageView) convertView.findViewById(R.id.select_image);
        ImageView x = (ImageView) convertView.findViewById(R.id.select_image_x);
        if (bean.isAddBtn()) {
            iv.setImageResource(R.drawable.add_image_btn);
            x.setVisibility(View.GONE);
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (maxSelectImageNumber >= 1) {
                        PhotoPicker.builder()
                                .setPhotoCount(maxSelectImageNumber)
                                .setShowCamera(true)
                                .setShowGif(true)
                                .setPreviewEnabled(false)
                                .start((Activity) mContext, PhotoPicker.REQUEST_CODE);
                    }
                }
            });

        } else {
            x.setVisibility(View.VISIBLE);
            ImageLoader.load(mContext, mDatas.get(position).getPath(), iv);
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean hasAddBtn = false;
                    ArrayList<String> list = new ArrayList<>();
                    for (int i = 0; i < mDatas.size(); i++) {
                        if (mDatas.get(i).isAddBtn()) {
                            hasAddBtn = true;
                        } else {
                            list.add(mDatas.get(i).getPath());
                        }
                    }

                    PhotoPreview.builder()
                            .setPhotos(list)
                            .setCurrentItem(hasAddBtn ? position - 1 : position)
                            .setShowDeleteButton(false)
                            .start((Activity) mContext);
                }
            });
            x.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDatas.remove(position);
                    if (maxSelectImageNumber == 0) {
                        SelectImageBean initBean = new SelectImageBean();
                        initBean.setAddBtn(true);
                        initBean.setPath("");
                        mDatas.add(initBean);
                    }
                    notifyDataSetChanged();
                    maxSelectImageNumber++;
                }
            });
        }
        return convertView;
    }
}
